<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        :destroy-on-close="true"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate == 'Add' ? '添加' : '编辑' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    ref="formRef"
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                    v-if="!baTable.form.loading"
                >
                    <FormItem label="头像"  type="image" v-model="baTable.form.items!.avatar" />

                    <FormItem
                        label="用户名"
                        v-model="baTable.form.items!.username"
                        type="string"
                        prop="username"
                        :input-attr="{
                            disabled: baTable.form.operate == 'Edit',
                        }"
                        placeholder="用户名建议为6-15的字母或者数字可以由纯数字"
                    />

                    <FormItem
                        label="权限分组"
                        v-model.number="baTable.form.items!.group_id"
                        prop="group_id"
                        type="remoteSelect"
                        :key="('group-' + baTable.form.items!.id)"
                        :input-attr="{
                            multiple: false,
                            params: { isTree: true, absoluteAuth: adminInfo.id == baTable.form.items!.id ? 0 : 1 },
                            field: 'name',
                            'remote-url': '/admin_api/user_group/get_select',
                            placeholder: '单击选择(默认普通用户)',
                        }"
                    />
                    <FormItem
                        label="姓名"
                        v-model="baTable.form.items!.name"
                        type="string"
                        prop="name"
                        placeholder="建议填写管理员真实姓名"
                    />
                    <FormItem
                        label="手机号"
                        prop="mobile"
                        v-model="baTable.form.items!.mobile"
                        type="string"
                        auto-complete="off"
                        placeholder="建议填写真实联系方式"
                    />
                    <FormItem
                        label="密码"
                        prop="password"
                        v-model="baTable.form.items!.password"
                        type="password"
                        auto-complete="off"
                        :placeholder="
                            baTable.form.operate == 'Add'
                                ? '建议为6-15的字母或者数字可以由纯数字'
                                : '如果未修改，请留空.建议为6-15的字母或者数字可以由纯数字'
                        "
                    />

                    <FormItem
                        label="状态"
                        v-model="baTable.form.items!.status"
                        type="radio"
                        :data="{ content: { '0': '禁用', '1': '启用' }, childrenAttr: { border: true } }"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">取消</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? '保存并编辑下一个' : '保存' }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, inject, watch } from 'vue'
import type baTableClass from '/src/utils/baTable'
import { regularPassword, buildValidatorData } from '/src/utils/validate'
import type { FormInstance, FormItemRule } from 'element-plus'
import FormItem from '/src/components/formItem/index.vue'
import { useAdminInfo } from '/src/stores/adminInfo'
import { useConfig } from '/src/stores/config'
import createAxios from "/src/utils/axios";

const config = useConfig()
const adminInfo = useAdminInfo()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass


const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    username: [buildValidatorData({ name: 'required', title: '用户名' }), buildValidatorData({ name: 'account' })],
    name: [buildValidatorData({ name: 'required', title: '姓名' })],
    group_id: [buildValidatorData({ name: 'required', message: '请选择管理员权限' })],
    mobile: [buildValidatorData({ name: 'mobile', message: '请输入正确格式的手机号' })],
    password: [
        {
            validator: (rule: any, val: string, callback: Function) => {
                if (baTable.form.operate == 'Add') {
                    if (!val) {
                        return callback(new Error('请输入密码'))
                    }
                } else {
                    if (!val) {
                        return callback()
                    }
                }
                if (!regularPassword(val)) {
                    return callback(new Error('请输入输入正确的密码'))
                }
                return callback()
            },
            trigger: 'blur',
        },
    ],
})

watch(
    () => baTable.form.operate,
    (newVal) => {
        rules.password![0].required = newVal == 'Add'
    }
)


</script>

<style scoped lang="scss">
.avatar-uploader {
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    border-radius: var(--el-border-radius-small);
    box-shadow: var(--el-box-shadow-light);
    border: 1px dashed var(--el-border-color);
    cursor: pointer;
    overflow: hidden;
    width: 110px;
    height: 110px;
}
.avatar-uploader:hover {
    border-color: var(--el-color-primary);
}
.avatar {
    width: 110px;
    height: 110px;
    display: block;
}
.image-slot {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
}
</style>
